package com.scnu409.mimicontact;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class TabDialog extends View {

	/**
	 * 私有变量
	 */
	private int mColor = 0xFFFFFFFF;
	private Bitmap mIconBitmap;
	private String mText = "微信";
	private int mTextSize = (int) TypedValue.applyDimension(
			TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics());

	private Canvas mCanvas;
	private Bitmap mBitmap;
	private Paint mPaint;

	private float mAlpha; // icon透明度

	private Rect mIconRect;
	private Rect mTextBound;
	private Paint mTextPaint;

	public TabDialog(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.TabDialog);

		int n = a.getIndexCount();

		for (int i = 0; i <n; i++) {
			int attr = a.getIndex(i);

			switch (attr) {
			case R.styleable.TabDialog_icon:
				BitmapDrawable mBitmapDrawable = (BitmapDrawable) a
						.getDrawable(attr);
				mIconBitmap = mBitmapDrawable.getBitmap();
				break;
			case R.styleable.TabDialog_text:
				mText = a.getString(attr);
				break;
			case R.styleable.TabDialog_color:
				mColor = a.getColor(attr, 0xFF45C01A);
				break;
			case R.styleable.TabDialog_text_size:
				mTextSize = (int) a.getDimension(attr, TypedValue
						.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12,
								getResources().getDisplayMetrics()));
				break;
			}
		} // for循环结束

		a.recycle();

		mTextBound = new Rect();
		mTextPaint = new Paint();
		mTextPaint.setTextSize(mTextSize);
		mTextPaint.setColor(0xff55555);
		mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);

	}

	public TabDialog(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TabDialog(Context context) {
		this(context, null);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int iconWidth = Math.min(getMeasuredWidth() - getPaddingLeft()
				- getPaddingRight(), getMeasuredHeight() - getPaddingTop()
				- getPaddingBottom() - mTextBound.height());

		// 横向居中
		int left = getMeasuredWidth() / 2 - iconWidth / 2;
		int top = (getMeasuredHeight() - mTextBound.height() - iconWidth) / 2;

		mIconRect = new Rect(left, top, left + iconWidth, top + iconWidth);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		canvas.drawBitmap(mIconBitmap, null, mIconRect, null);
		//内存去除准备mBitmap,setAlpha,纯色，xfermode,图标
		
		int alpha=(int)Math.ceil(255*mAlpha);
		setTargetBitmap(alpha);
		//canvas.drawBitmap(mBitmap, 0, 0, null);  //设置图标
		//设置文本
		setSourceText(canvas,alpha);
		setDrawText(canvas,alpha);
	}

	/**
	 * 绘制变色文本
	 * 
	 * @param canvas
	 * @param alpha
	 */
	private void setDrawText(Canvas canvas, int alpha) {
		mTextPaint.setColor(Color.WHITE);
		mTextPaint.setAlpha(alpha);
		int x = getMeasuredWidth() / 2 - mTextBound.width() / 2; // 文本显示的位置的x轴
		int y = mIconRect.bottom + mTextBound.height(); // 文本现实的位置的y轴
		canvas.drawText(mText, x, y, mTextPaint);
	}

	/**
	 * 绘制原文本
	 * 
	 * @param canvas
	 */
	private void setSourceText(Canvas canvas, int alpha) {
		mTextPaint.setColor(Color.WHITE);
		mTextPaint.setAlpha(255 - alpha);
		int x = getMeasuredWidth() / 2 - mTextBound.width() / 2; // 文本显示的位置的x轴
		int y = mIconRect.bottom + mTextBound.height(); // 文本现实的位置的y轴
		canvas.drawText(mText, x, y, mTextPaint);
	}

	/**
	 * 创建可变色的icon 通过改变滑动icon改变alpha
	 */
	protected void setTargetBitmap(int alpha) {
		mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
				Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		mPaint = new Paint();
		mPaint.setColor(mColor);
		mPaint.setDither(true); // 设置图片进行抖动处理，使图片更加平滑
		mPaint.setAntiAlias(true); // 设置图片进行抗锯齿处理
		mPaint.setAlpha(alpha);
		mCanvas.drawRect(mIconRect, mPaint);
		mPaint.setAlpha(255);
		// src为前景，dst为背景
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); // 设置两个ICON相交时的模式
		mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
	}
	
	/**
	 * 设置让别的动作改变icon的alpha
	 * @param alpha
	 */
	public void setIconAlpha(float alpha){
		this.mAlpha=alpha;
		invalidateView(alpha);  //重绘
	}

	private void invalidateView(float alpha) {
		//判断是否是UI线程
		if(Looper.getMainLooper()==Looper.myLooper()){
			invalidate();  //只能在主线程中使用
		}
		else
			postInvalidate();  //在工作线程中使用
	}

}
